package com.easytouch.vmhdl.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.easytouch.core.utils.Log;
import com.easytouch.vmhdl.AppApplication;

import java.io.File;

/**
 * Created by gulin on 2017/12/26.
 */

public class GoodsImageView extends View {

    private static final int MOVE_DIR_NONE = 0;
    private static final int MOVE_DIR_TO_RIGHT = 1;
    private static final int MOVE_DIR_TO_LEFT = 2;

    private int width;
    private int height;
    private Paint mPaint = new Paint();
    private String mGoodsId;
    private int mImageCount = 0;
    private int mCurrentIndex = 0;
    private int mDir = MOVE_DIR_NONE;
    private float x;
    private float y;
    private boolean isMoving = false;

    private MyHandler mHandler;

    private Log mLogger = new Log("GoodsImageView.txt");

    public GoodsImageView(Context context) {
        super(context);
        init();
    }

    public GoodsImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GoodsImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
    }

    public boolean setGoods(final String id)
    {
        mImageCount = 0;
        mCurrentIndex = 0;
        mDir = MOVE_DIR_NONE;
        isMoving = false;
        if(id == null || "".equals(id))
        {
            return false;
        }
        for (int i = 0;i < 100;i++) {
            File pngFile = new File(AppApplication.path + "/images/goods_image/" + id + "/" + i + ".png");
            if(!pngFile.exists())
            {
                break;
            }
            mImageCount++;
        }
        if(mImageCount < 2)
        {
            return false;
        }
        mGoodsId = id;
        postInvalidate();
        if(mHandler == null)
        {
            mHandler = new MyHandler();
        }
        mHandler.sendEmptyMessageDelayed(0, 40);
        return true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
    }

    @Override
    public void draw(final Canvas canvas) {
        super.draw(canvas);
        if(mGoodsId == null || "".equals(mGoodsId))
        {
            return;
        }
        Bitmap dstbmp = BitmapFactory.decodeFile(AppApplication.path + "/images/goods_image/" + mGoodsId + "/" + String.valueOf(mCurrentIndex) + ".png");
        canvas.drawBitmap(dstbmp, 0, 0, mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            mLogger.i("ACTION_DOWN");
            x = event.getX();
            y = event.getY();
            if(isMoving)
            {
                isMoving = false;
            }
            else
            {
                mDir = MOVE_DIR_TO_RIGHT;
                isMoving = true;
            }
            //mLogger.i("x:" + String.valueOf(x) + " y:" + String.valueOf(y));
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            mLogger.i("ACTION_UP");
            if(event.getX() > x) //move to right
            {
                mDir = MOVE_DIR_TO_RIGHT;
            }
            else
            {
                mDir = MOVE_DIR_TO_LEFT;
            }
            if(Math.abs(event.getX() - x) <= 5)
            {
                isMoving = false;
            }
            else {
                isMoving = true;
            }
            mLogger.i("event x:" + String.valueOf(x) + " event y:" + String.valueOf(y));
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            mLogger.i("ACTION_MOVE");
            if(event.getX() > x) //move to right
            {
                mDir = MOVE_DIR_TO_RIGHT;
            }
            else
            {
                mDir = MOVE_DIR_TO_LEFT;
            }
            if(Math.abs(event.getX() - x) <= 5)
            {
                isMoving = false;
            }
            else {
                isMoving = true;
            }
            mLogger.i("event x:" + String.valueOf(x) + " event y:" + String.valueOf(y));
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDetachedFromWindow() {
        if(mHandler != null)
        {
            mHandler.removeMessages(0);
            mHandler = null;
        }
        super.onDetachedFromWindow();
    }

    class MyHandler extends Handler
    {

        public MyHandler()
        {
        }

        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 0: //定时滚动
                {
                    if(isMoving)
                    {
                        if(mDir == MOVE_DIR_TO_RIGHT)
                        {
                            mCurrentIndex++;
                            if(mCurrentIndex >= mImageCount)
                            {
                                mCurrentIndex = 0;
                            }
                            postInvalidate();
                        }
                        else if(mDir == MOVE_DIR_TO_LEFT)
                        {
                            mCurrentIndex--;
                            if(mCurrentIndex < 0)
                            {
                                mCurrentIndex = mImageCount - 1;
                            }
                            postInvalidate();
                        }
                    }
                    sendEmptyMessageDelayed(0, 40);
                    //mLogger.i("handleMessage");
                    break;
                }
            }
        }
    }
}
